namespace Core.Features
{
    using System;
    using System.Linq;
    using Base;
    using Contracts.Enums;
    using Resources;
    using Contracts.Interfaces;

    
    public sealed class RobotMass<T> : FeatureBase<object>
    {
        public RobotMass(){}

        public RobotMass(T value) 
            : base(value){ }
        /// <summary>
        /// Obtains the feature name
        /// </summary>
        public override string Name
        {
            get { return "Mass"; }
        }
        /// <summary>
        /// Obtains the feature group
        /// </summary>
        public override FeatureGroupsEnum FeatureGroup{ get { return FeatureGroupsEnum.General; } }
        /// <summary>
        /// Obtains the feature type
        /// </summary>
        public override FeaturesEnum FeatureType { get { return FeaturesEnum.RobotMass; } }
        /// <summary>
        /// Obtains or defines the feature unit
        /// </summary>
        public override string Unit { get { return Traduction.label_unit_kg; } }

        /// <summary>
        /// Determines if the feature value is rounded
        /// </summary>
        public override bool IsRounded
        {
            get { return true; }
        }

        public override void SetModifiedValue(IAgent agent, IRobot robot)
        {
            if (agent == null || robot == null) return;

            double temp = robot.Modules
                .Where(m => m.Modifiers != null && m.Modifiers.Any())
                .Select(module => module.Modifiers.Where(m => m.ModifiedFeatureType == FeatureTypesEnum.Robot && m.Feature == this.FeatureType && m.Value > 0))
                .Where(mods => mods.Any())
                .Sum(mods => mods.Sum(modifier => modifier.Value));

            double lwfMass = 0;
  
            foreach (var module in robot.Modules.Where(m => m.Modifiers != null && m.Modifiers.Any()))
            {
                var lwf = module.Modifiers.Where( m => m.ModifiedFeatureType == FeatureTypesEnum.Robot && m.Feature == this.FeatureType && m.Value < 0).FirstOrDefault();
                if(lwf==null) continue;

                lwfMass = (double)Value * Math.Abs(lwf.Value);
            }

            ModifiedValue = IsRounded
                                ? Math.Round((double)Value + temp - lwfMass, 0, MidpointRounding.AwayFromZero)
                                : Math.Round((double)Value + temp - lwfMass, 2);
        }
    }
}